﻿Imports OMANE.Common.UI
Imports System.Reflection
Imports System.ComponentModel
Imports System.Threading
Imports System.Windows.Threading

Module StartModule


    Private WpfApp As WpfApp
    

    Private Sub AppDomainUnhandledException(ByVal sender As Object, ByVal e As UnhandledExceptionEventArgs)
        Dim ui = App.Instance.GetService(Of IUIService)()
        ui.ShowMessage(CType(e.ExceptionObject, Exception), "Un probleme est survenu ....", False)

    End Sub

    Private Sub AppDomainCurrentDomainFirstChanceException(ByVal sender As Object, ByVal e As Runtime.ExceptionServices.FirstChanceExceptionEventArgs)
        IO.File.AppendAllText("log3.txt", _
         "[DEBUT]" & vbCrLf & _
         DateTime.Now.ToString("dd/MM/yyy hh:mm") & " : " & e.Exception.Message & _
         vbCrLf & _
         "StackTrace:" & e.Exception.StackTrace & vbCrLf & _
         "[FIN]" & vbCrLf)
    End Sub

    Private Sub WpfAppDispatcherUnhandledException(ByVal sender As Object, ByVal e As Windows.Threading.DispatcherUnhandledExceptionEventArgs)
        Dim ui = App.Instance.GetService(Of IUIService)()
        ui.ShowMessage(e.Exception, "Un probleme est survenu ....", False)
        e.Handled = True
    End Sub

    Private Sub LastError()
        Dim temp = New WpfApp()
        temp.InitializeComponent()
        Dim data As Object = AppDomain.CurrentDomain.GetData("exception")
        temp.Run(New MessageWindow() With {.DataContext = data})
    End Sub

    <STAThread()> _
    Public Sub Main(ByVal ParamArray args() As String)

        'Dim app = New WpfApp()
        'app.InitializeComponent()
        'app.Run(New ViewMainWindow(Nothing))
        'Return
        'Dim mod2 = New StartModuleV2()

        'mod2.Main(args)


        'Return
        'IO.File.AppendAllText("log3.txt", "<--------Lancement de OMANE à " & DateTime.Now.ToString("dd/MM/yyy hh:mm") & "------->" & vbCrLf & vbCrLf)
        'AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf AppDomainUnhandledException
        AddHandler AppDomain.CurrentDomain.FirstChanceException, AddressOf AppDomainCurrentDomainFirstChanceException



        WpfApp = New WpfApp()
        AddHandler WpfApp.DispatcherUnhandledException, AddressOf WpfAppDispatcherUnhandledException
        Try
            WpfApp.InitializeComponent()
            'WpfApp.Run(New ViewMainWindow(New MainWindowViewModel()))
            WpfApp.Run(New ViewMainWindow(Nothing))
        Catch ex As Exception
            'IO.File.WriteAllText("log.txt", ex.ToString())
            Try
                'RemoveHandler AppDomain.CurrentDomain.UnhandledException, AddressOf AppDomainUnhandledException
                'RemoveHandler WpfApp.DispatcherUnhandledException, AddressOf WpfAppDispatcherUnhandledException
                'RemoveHandler AppDomain.CurrentDomain.FirstChanceException, AddressOf AppDomainCurrentDomainFirstChanceException
                WpfApp.Shutdown()
                WpfApp = Nothing
                Dim NewDomain = AppDomain.CreateDomain("newdomain")
                NewDomain.SetData("exception", ex)
                NewDomain.DoCallBack(New CrossAppDomainDelegate(AddressOf LastError))
            Catch ex2 As Exception
                'IO.File.WriteAllText("log2.txt", ex2.ToString())
            End Try
        End Try
        'IO.File.AppendAllText("log3.txt", vbCrLf & "<--------Fin de OMANE à " & DateTime.Now.ToString("dd/MM/yyy hh:mm") & "------->" & vbCrLf)
    End Sub
End Module
Public Delegate Sub AsyncInitializationHandler(ByVal args() As String)